﻿// -----------------------------------------------------------------------
// <copyright file="ResultNotificationMessage.cs" company="Zen Design Corp">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Zen.BlackBerry.Pap.Parser
{
	using System;
	using System.Xml;
	using System.Xml.Schema;
	using System.Xml.Serialization;
	using Zen.BlackBerry.Common;

	/// <summary>
	/// Represents a PAP result notification.
	/// </summary>
	public class ResultNotification : PapMessage, IXmlSerializable
	{
		/// <summary>
		/// Gets or sets the sender address.
		/// </summary>
		/// <value>The sender address.</value>
		public string SenderAddress
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the name of the sender.
		/// </summary>
		/// <value>The name of the sender.</value>
		public string SenderName
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the received time.
		/// </summary>
		/// <value>The received time.</value>
		public DateTime? ReceivedTime
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the event time.
		/// </summary>
		/// <value>The event time.</value>
		public DateTime? EventTime
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the state of the message.
		/// </summary>
		/// <value>The state of the message.</value>
		public MessageState MessageState
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the code.
		/// </summary>
		/// <value>The code.</value>
		public StatusCode Code
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the description.
		/// </summary>
		/// <value>The description.</value>
		public string Description
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the address.
		/// </summary>
		/// <value>The address.</value>
		public string Address
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the quality of service.
		/// </summary>
		/// <value>The quality of service.</value>
		public QualityOfService QualityOfService
		{
			get;
			set;
		}

		#region IXmlSerializable Members

		/// <summary>
		/// This method is reserved and should not be used. When implementing the IXmlSerializable interface, you should return null (Nothing in Visual Basic) from this method, and instead, if specifying a custom schema is required, apply the <see cref="T:System.Xml.Serialization.XmlSchemaProviderAttribute"/> to the class.
		/// </summary>
		/// <returns>
		/// An <see cref="T:System.Xml.Schema.XmlSchema"/> that describes the XML representation of the object that is produced by the <see cref="M:System.Xml.Serialization.IXmlSerializable.WriteXml(System.Xml.XmlWriter)"/> method and consumed by the <see cref="M:System.Xml.Serialization.IXmlSerializable.ReadXml(System.Xml.XmlReader)"/> method.
		/// </returns>
		public XmlSchema GetSchema()
		{
			return null;
		}

		/// <summary>
		/// Generates an object from its XML representation.
		/// </summary>
		/// <param name="reader">The <see cref="T:System.Xml.XmlReader"/> stream from which the object is deserialized.</param>
		public void ReadXml(XmlReader reader)
		{
			PushId = reader["push-id"];
			Code = StatusCode.GetCode(reader["code"]);
			Description = reader["desc"];
			SenderAddress = reader["sender-address"];
			SenderName = reader["sender-name"];

			ReceivedTime = reader.ReadRimTime("received-time");
			EventTime = reader.TryReadRimTime("event-time");

			MessageState = reader.ReadEnum<MessageState>("message-state", MessageState.Unknown);

			using (XmlReader subElementReader = reader.ReadSubtree())
			{
				if (subElementReader.ReadToDescendant("address"))
				{
					Address = subElementReader["address-value"];
				}
			}

			using (XmlReader subElementReader = reader.ReadSubtree())
			{
				if (subElementReader.ReadToDescendant("quality-of-service"))
				{
					XmlSerializer ser = new XmlSerializer(typeof(QualityOfService));
					QualityOfService = (QualityOfService)ser.Deserialize(subElementReader);
				}
			}
		}

		/// <summary>
		/// Converts an object into its XML representation.
		/// </summary>
		/// <param name="writer">The <see cref="T:System.Xml.XmlWriter"/> stream to which the object is serialized.</param>
		public void WriteXml(XmlWriter writer)
		{
		}

		#endregion
	}
}
